# 
# little_rails_app.rb
# 
# Created on 20/09/2007, 09:50:59
# EmmanuelOga.blogspot.com

require 'set'
require 'ferret'
require 'active_support'
require 'action_pack'
require 'action_controller'
require 'action_view'
require 'active_record'

# For testing, i require these manually (rails would load it automatically 
# when using the ar_spec_scaffold generator cause it places the file in the "right" place)

# labels
require File.join(File.dirname(__FILE__), '../generators/ar_spec_scaffold/templates/labels.rb')

# default methods
current_dir= File.dirname(File.expand_path(__FILE__))
files= Dir[File.join(current_dir, '../generators/ar_spec_scaffold/templates/default_methods/*.rb')]
files.each {|f| require f}

# Load the plugin.
require File.join(File.dirname(__FILE__), '../init.rb')

# ------------------------------------------------------------------------------
ActionController::Routing::Routes.draw do |map|
  map.connect '', :controller => "zones"
  
  search_opt= { :search => :get }
  
  # The controller for this resource will be configured "by hand"
  # See options_assignation_test.rb
  map.resources :resources, :collection => search_opt
  
  map.resources :sales_men, :collection => search_opt do |sm|
    sm.resources :documents, :name_prefix => "sales_man_" 
    sm.resources :customers, :name_prefix => "sales_man_" 
  end

  map.resources :zones, :collection => search_opt do |zone|
    zone.resources :customers, :name_prefix => "zone_" 
  end
  
  map.resources :customers, :collection => search_opt do |customer|
    customer.resources :documents, :name_prefix => "customer_" 
  end
  
  map.resources :documents, :collection => search_opt
end

# ------------------------------------------------------------------------------
# connect to the database (sqlite in this case)
ActiveRecord::Base.establish_connection({
  :adapter => "sqlite3",
  :dbfile => ":memory:"
})

ActiveRecord::Schema.define(:version => 3) do
  create_table "sales_men", :force => true do |t|
    t.column "name",           :string
  end
  
  create_table "customers", :force => true do |t|
    t.column "name",           :string
    t.column "is_distributor", :boolean
    t.column "zone_id",        :integer
    t.column "sales_man_id",   :integer
  end

  create_table "documents", :force => true do |t|
    t.column "customer_id", :integer
    t.column "sales_man_id",:integer
    t.column "name",        :string
    t.column "content",     :text
  end

  create_table "zones", :force => true do |t|
    t.column "name",       :string
    t.column "created_at", :datetime
  end
end

# ------------------------------------------------------------------------------
class Zone < ActiveRecord::Base
  has_many :customers
end

class SalesMan <  ActiveRecord::Base
  has_many :customers
  has_many :documents
end

class Customer < ActiveRecord::Base
  belongs_to :zone
  belongs_to :sales_man
  has_many :documents
end

class Document < ActiveRecord::Base
  belongs_to :customer
  belongs_to :sales_man
end

# ------------------------------------------------------------------------------

unless defined? CUSTOM_TEMPLATES 
  CUSTOM_TEMPLATES= returning(Struct::ARTemplates.new) do |templates|
    d= File.dirname(__FILE__)
    %w|index show edit new search|.each do |template|
      p= File.join(d, "../generators/auto_rest/templates/#{template}")
      templates.send("#{template}=", { :file =>  p})
    end
  end
end

# Default templates on views.
unless defined? DEFAULT_TEMPLATES
  DEFAULT_TEMPLATES= returning(Struct::ARTemplates.new) do |templates|
    %w|index show edit new search|.each do |template|
      templates.send("#{template}=", { :template => "auto_rest_shared/#{template}" })
    end
    templates.index_partial={:partial=>"auto_rest_shared/index_items", :layout=>false}
    templates.search_results={:partial=>"auto_rest_shared/search_results", :layout=>false}
  end
end

# This will be handled by rails auto-require when used on a real app.
Dir["../generators/ar_spec_scaffold/templates/default_methods/*.rb"].each do |file|
  require file
end

class ApplicationController  < ActionController::Base
end

class ZonesController < ApplicationController
  create_auto_rest do |opt|
    opt.templates= CUSTOM_TEMPLATES
  end
end

class CustomersController < ApplicationController 
  create_auto_rest do |opt|
    opt.templates= CUSTOM_TEMPLATES
  end
end

class SalesMenController < ApplicationController 
  create_auto_rest 
end

class DocumentsController < ApplicationController
  create_auto_rest
end

#-------------------------------------------------------------------------------

# Return defaults for Ferret option
def ferret_addon
  Struct::ARAddon.new(true, { :search_limit => :all })
end

# Return defaults for WillPaginage option
def wp_addon
  wp_enabled= defined?(WillPaginate) == "constant"
  default_wp_options= !wp_enabled ? nil : {
    :prev_label => AutoRest::Labels.defaults[:pagination_prev], 
    :next_label => AutoRest::Labels.defaults[:pagination_next],
    :inner_window => 2,
    :outer_window => 1 
  }
  Struct::ARAddon.new(wp_enabled, default_wp_options)
end
